package com.pubinfo.passbook.api.controller.auth;

import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.api.eip.UserInfoService;
import com.pubinfo.passbook.api.vat.ForwardVATService;
import com.pubinfo.passbook.common.mapper.TFileElectronicFtpMapper;
import com.pubinfo.passbook.common.mapper.TFileEnclosureFtpMapper;
import com.pubinfo.passbook.common.mapper.TRuleUserWiteoffMapper;
import com.pubinfo.passbook.common.mapper.TWiteoffInstanceMapper;
import com.pubinfo.passbook.common.model.dto.witeoff.MedicineOnJobOrRetireDTO;
import com.pubinfo.passbook.common.model.http.GetReimbursementInformation.ReimbursementInformation;
import com.pubinfo.passbook.common.model.http.GetReimbursementInformation.WriteoffInstanceCode;
import com.pubinfo.passbook.common.model.http.request.GainWriteoffInstStatus.GainWriteoffInstStatusItems;
import com.pubinfo.passbook.common.model.http.request.GetWriteoffBaseData.GetWriteoffBaseDataItems;
import com.pubinfo.passbook.common.model.http.response.GetWriteoffBaseData.WriteoffBaseDataMessage;
import com.pubinfo.passbook.common.model.http.response.GetWriteoffInstStatus.WriteoffInstStatusMessage;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.service.dedicated.DedicatedService;
import com.pubinfo.passbook.common.service.mss.HttpService;
import com.pubinfo.passbook.common.service.mss.WriteoffInstanceService;
import com.pubinfo.passbook.common.service.ruler.CheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "mss")
@Api(tags = "财辅相关接口")
@Slf4j
public class MssController {
    @Autowired
    WriteoffInstanceService writeoffInstanceService;
    @Autowired
    HttpService httpService;

    @Resource(name = "httpServiceWithEngineImpl")
    HttpService httpServiceWithEngineImpl;

    @Autowired
    DedicatedService dedicatedService;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    ForwardVATService vatService;
    @Autowired
    CheckService checkService;

    @Autowired
    @Qualifier("ruleEngineCheckServiceImpl")
    CheckService ruleEngineCheckService;


    @Autowired
    TFileEnclosureFtpMapper tFileEnclosureFtpMapper;
    @Autowired
    TWiteoffInstanceMapper tWiteoffInstanceMapper;
    @Autowired
    TFileElectronicFtpMapper tFileElectronicFtpMapper;
    @Autowired
    TRuleUserWiteoffMapper witeoffMapper;

    @PostMapping("getReimbursementInformationByDate/{date}")
    @ApiOperation(value = "通过日期获取报账单状态信息")
    public JsonResult<ReimbursementInformation> getReimbursementInformationByDate(@PathVariable String date) throws UnirestException, ParseException {
        return JsonResult.success(writeoffInstanceService.getReimbursementInformationByDate(date));
    }

    @PostMapping("getReimbursementInformationByWriteoffInstanceCode")
    @ApiOperation(value = "通过报账单号获取报账单状态信息")
    public JsonResult<ReimbursementInformation> getReimbursementInformationByWriteoffInstanceCode(@RequestBody List<WriteoffInstanceCode> list) throws UnirestException, ParseException {
        return JsonResult.success(writeoffInstanceService.getReimbursementInformationByWriteoffInstanceCode(list));
    }

    @PostMapping("getWriteoffBaseDataItem")
    @ApiOperation(value = "获取报账组织信息")
    public JsonResult<WriteoffBaseDataMessage> getWriteoffBaseDataItem(@RequestBody ArrayList<GetWriteoffBaseDataItems> account) throws IOException, UnirestException {

        return JsonResult.success(dedicatedService.getWriteoffBaseDataItem(account));
    }

    @PostMapping("getWriteoffInstStatusBodyItem")
    @ApiOperation(value = "获取报账单状态")
    public JsonResult<WriteoffInstStatusMessage> getWriteoffInstStatusBodyItem(@RequestBody ArrayList<GainWriteoffInstStatusItems> account) {
        return JsonResult.success(dedicatedService.getWriteoffInstStatusBodyItem(account));
    }

    @PostMapping("medicineOnJobAutoCreateWriteoff")
    @ApiOperation(value = "在职及供属医药费报账 普通报账接口")
    public JsonResult<String> medicineOnJobAutoCreateWriteoff(@RequestBody MedicineOnJobOrRetireDTO jobDTO) {
        return httpService.medicineOnJobAutoCreateWriteoff(jobDTO, false, userInfoService.getCurrentAcctId());
    }

    @PostMapping("medicineOnJobAutoCreateWriteoffWithEngine")
    @ApiOperation(value = "在职及供属医药费报账 普通报账接口 (引擎Beta)")
    public JsonResult<String> medicineOnJobAutoCreateWriteoffWithEngine(@RequestBody MedicineOnJobOrRetireDTO jobDTO) {
        return httpServiceWithEngineImpl.medicineOnJobAutoCreateWriteoff(jobDTO, false, userInfoService.getCurrentAcctId());
    }


//    @PostMapping("medicineRetireAutoCreateWriteoff")
//    @ApiOperation(value = "退休员工医药费报账")
//    public JsonResult<String> medicineRetireAutoCreateWriteoff(@RequestBody MedicineOnJobOrRetireDTO jobDTO) {
//        return vatService.medicineOnJobAutoCreateWriteoff(jobDTO);
//    }

    @PostMapping("countWriteoff")
    @ApiOperation(value = "计算报账金额")
    public JsonResult<BigDecimal> countWriteoff(@RequestBody MedicineOnJobOrRetireDTO retireDTO) {
        if (CollectionUtils.isEmpty(retireDTO.getInvoiceIds())) {
            return JsonResult.error("票据不能为空");
        }
        return JsonResult.success(checkService.checkPassbookSum(retireDTO), retireDTO.getDetalis());
    }

    @PostMapping("countWriteoffWithEngine")
    @ApiOperation(value = "计算报账金额 (引擎Beta)")
    public JsonResult<BigDecimal> countWriteoffWithEngine(@RequestBody MedicineOnJobOrRetireDTO retireDTO) {
        if (CollectionUtils.isEmpty(retireDTO.getInvoiceIds())) {
            return JsonResult.error("票据不能为空");
        }
        return JsonResult.success(ruleEngineCheckService.checkPassbookSum(retireDTO), retireDTO.getDetalis());
    }

    @PostMapping("writeoffFile")
    @ApiOperation(value = "根据报账单id查询所有用户上传的文件")
    public JsonResult<List<String>> writeoffFile(@RequestBody String witeoffInstanceId) {
        return JsonResult.success(tFileEnclosureFtpMapper.selectEnclosureAcctAdd(witeoffInstanceId));
    }

    @PostMapping("passbookAmountSum")
    @ApiOperation(value = "根据用户账号与费用类型查询本年未报销票据总金额")
    public JsonResult<String> passbookAmountSum(String acct, String type) {
        return JsonResult.success(httpService.passbookAmountSum(acct, type));
    }

//  @PostMapping("groupMembershipDuesAutoCreateWriteoff")
//  @ApiOperation(value = "社会团体会费报账")
//  public JsonResult<String> groupMembershipDuesAutoCreateWriteoff(@RequestBody EnvironmentalOrGroupMembershipDuesDTO duesDTO) throws IOException, UnirestException {
//        return httpService.groupMembershipDuesAutoCreateWriteoff(duesDTO);
//    }
//    @PostMapping("environmentalAutoCreateWriteoff")
//    @ApiOperation(value = "环保费报账")
//    public JsonResult<String> environmentalAutoCreateWriteoff(@RequestBody EnvironmentalOrGroupMembershipDuesDTO duesDTO) throws IOException, UnirestException {
//        return httpService.environmentalAutoCreateWriteoff(duesDTO);
//    }

//  @PostMapping("synWriteoffInfo")
//  @ApiOperation(value = "同步报账单信息")
//  public JsonResult<OutParam> synWriteoffInfo(@RequestBody InputParamBody inputParamBody) throws IOException, UnirestException {
//    return JsonResult.success(httpService.synWriteoffInfo(inputParamBody));
//  }

}
